﻿using System;
using UnityConfiguration.Infrastructure;
using System.Windows.Markup;
using mpu = Microsoft.Practices.Unity;
using System.Collections.Generic;
using System.Linq;

namespace UnityConfiguration
{
    /// <summary>
    /// Configuration element for configuring constructor injection.
    /// </summary>
    [ContentProperty("Items")]
    public class Constructor : InjectionMember, ITypeConfigurationItem
    {
        private ParameterCollection parameters = new ParameterCollection();
        /// <summary>
        /// Name of the element.
        /// </summary>
        public override string Name {
            get { return ""; }
            set { }
        }

        public List<Parameter> Items {
            get { return parameters.ToList(); }
            set { parameters = new ParameterCollection(value); }
        }

        /// <summary>
        /// The collection of <see cref="InjectionParameterValue"/> elements
        /// that are children of this node.
        /// </summary>
        public ParameterCollection Parameters {
            get {
                if (null != parameters) {
                    parameters.TypeResolver = TypeResolver;
                }
                return parameters;
            }
        }
        /// <summary>
        /// Return the InjectionMember object represented by this configuration
        /// element.
        /// </summary>
        /// <returns>The injection member object.</returns>
        public override mpu.InjectionMember CreateInjectionMember() {
            mpu.InjectionParameterValue[] values = new mpu.InjectionParameterValue[Parameters.Count];
            int i = 0;
            foreach (var element in Parameters) {
                values[i++] = element.CreateInjectionParameterValue();
            }
            return new mpu.InjectionConstructor(values);
        }
    }
}
